package com.jlastu.dlxy_androidapp.adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.contract.GoodsManageContract;
import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.presenter.GoodsManagePresenter;
import com.jlastu.dlxy_androidapp.view.details.CommodityDetails;

import java.util.List;

public class GoodsManageListAdapter extends RecyclerView.Adapter<GoodsManageListAdapter.ViewHolder> {

private GoodsManageContract.IGoodsManagePresenter mGoodsManagePresenter;
   private List<Goods> mList;

   private List<Photo> mList_P;


   public GoodsManageListAdapter(List<Goods> list , List<Photo> list_P,GoodsManageContract.IGoodsManagePresenter GoodsManagePresenter){
      mList = list;
      mList_P = list_P;
      mGoodsManagePresenter = GoodsManagePresenter;
   }

   @SuppressLint("NotifyDataSetChanged")
   public void updateData(List<Goods> list, List<Photo> list_P) {
      mList.clear();
      mList = list;
      mList_P=list_P;
      notifyDataSetChanged();
   }

   @NonNull
   @Override
   public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goodsmanage_item, null);

      ViewHolder viewHolder = new ViewHolder(view,mGoodsManagePresenter);
      return viewHolder;
   }


   @SuppressLint("DefaultLocale")
   public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
      Goods goods = mList.get(position);
      holder.textView1.setText(goods.getName());
      holder.textView2.setText(goods.getClassify());
      if ( position >= mList.size() || position >= mList_P.size()) {
         // 显示错误图标
          holder.photo.setImageResource(R.drawable.attention);
         return;
      }
      Photo photo = mList_P.get(position);
      holder.photo.setImageBitmap(photo.getBitmap());
   }

   @Override
   public int getItemCount() {
      return mList.size();
   }

   public class ViewHolder extends RecyclerView.ViewHolder {
      ImageView photo;
      TextView textView1;
      TextView textView2;

      public ViewHolder(@NonNull View itemView, GoodsManageContract.IGoodsManagePresenter mGoodsManagePresenter) {
         super(itemView);
         itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // 获取点击的列表项位置
               int position = getAdapterPosition();
               if (position != RecyclerView.NO_POSITION) {
                  // 处理点击事件，例如启动新的Activity
                  Goods goods = mList.get(position);
                  Intent intent = new Intent(itemView.getContext(), CommodityDetails.class);
                  intent.putExtra("goods_id", goods.getId());
                  itemView.getContext().startActivity(intent);

               }
            }
         });
         itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
               int position = getAdapterPosition();
               if (position != RecyclerView.NO_POSITION) {
                  // 获取当前位置的Goods对象
                  Goods goods = mList.get(position);
                  // 显示删除确认的AlertDialog
                  new AlertDialog.Builder(itemView.getContext())
                          .setTitle("确认删除")
                          .setMessage("您确定要删除 " + goods.getName() + " 吗？")
                          .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog, int which) {
                                // 用户点击确定，执行删除操作
                                mGoodsManagePresenter.deleteGoods(goods.getId());
                                // 从列表中移除项
                                mList.remove(position);
                                // 通知Adapter项已移除
                                notifyItemRemoved(position);
                                // 如果有照片列表，也从其中移除
                                if (mList_P != null && mList_P.size() > position) {
                                   mList_P.remove(position);
                                }
                             }
                          })
                          .setNegativeButton("取消", null)
                          .create()
                          .show();
                  return true; // 消费了长按事件
               }
               return false;
            }


         });
         photo = itemView.findViewById(R.id.goods_photo);
         textView1 = itemView.findViewById(R.id.goods_name);
         textView2 = itemView.findViewById(R.id.goods_classify);
      }
   }
}
